package com.shunbo.convenient.controller;


import com.shunbo.convenient.service.ProductTypeService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.ProductType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author weizhuang
 * 分类 接口
 */
@RestController
@RequestMapping("/productType")
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;


    /**
     * 分类列表
     *
     * @param productType 分类
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getProductTypeList(@RequestBody ProductType productType) {
        try {
            List<ProductType> all = productTypeService.findAll(productType);
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加分类
     *
     * @param productType 分类
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveProductType(@RequestBody ProductType productType) {
        try {
            Integer save = productTypeService.save(productType);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新分类
     *
     * @param productType 分类
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateProductType(@RequestBody ProductType productType) {
        try {
            Integer save = productTypeService.update(productType);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }
    /**
     * 删除分类
     *
     * @param productType 分类
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteProductType(@RequestBody ProductType productType) {
        try {
            Integer save = productTypeService.delete(productType.getId()+"");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

}
